package org.yiqixue.accounts.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yiqixue.accounts.dto.ErrorResponseDto;
import org.yiqixue.accounts.dto.TransactionDto;
import org.yiqixue.accounts.service.ITransactionService;

import java.util.List;

// Transaction 控制器类，处理与交易相关的 REST 请求
@Tag(
        name = "Transaction REST APIs",
        description = "REST APIs to manage transactions"
)
@RestController
@RequestMapping(path = "/api/transactions", produces = {MediaType.APPLICATION_JSON_VALUE})
public class TransactionController {

    private final ITransactionService transactionService;

    public TransactionController(ITransactionService transactionService) {
        this.transactionService = transactionService;
    }

    // 根据手机号和交易类型查询当日的所有交易记录
    @Operation(
            summary = "Get transactions by mobile number and type for today",
            description = "Get all transactions of a specific type for a customer's account today based on mobile number"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "200",
                    description = "HTTP Status OK"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "HTTP Status Internal Server Error",
                    content = @Content(
                            schema = @Schema(implementation = ErrorResponseDto.class)
                    )
            )
    })
    @GetMapping
    public ResponseEntity<List<TransactionDto>> getTransactionsByMobileNumberAndType(
            @RequestParam String mobileNumber,
            @RequestParam String type
    ) {
        // 调用服务层方法获取交易记录
        List<TransactionDto> transactions = transactionService.getTransactionsByMobileNumberAndType(mobileNumber, type);
        return ResponseEntity.status(HttpStatus.OK).body(transactions);
    }

    // 插入一条交易记录
    @Operation(
            summary = "Create a new transaction",
            description = "Create a new transaction for a customer's account"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "201",
                    description = "HTTP Status Created"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "HTTP Status Internal Server Error",
                    content = @Content(
                            schema = @Schema(implementation = ErrorResponseDto.class)
                    )
            )
    })
    @PostMapping
    public ResponseEntity<TransactionDto> createTransaction(@Valid @RequestBody TransactionDto transactionDto) {
        // 调用服务层方法创建交易记录
        TransactionDto createdTransaction = transactionService.createTransaction(transactionDto);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdTransaction);
    }
}